package caddey.jonathan.gameframeworkandroidimplementation;

import java.io.IOException;

import android.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import framework.ImageWrapper;
import framework.ResourcesWrapper;

public class AndroidResources implements ResourcesWrapper {

	/*TODO This reference prevents the context from being
	 * garbage collected when the screen rotates,
	 * so resources should not be held onto for long.
	 */
	private final Context my_context;
	public AndroidResources(Context the_context) {
		my_context = the_context;
	}
	@Override
	public ImageWrapper loadImage(String the_path) {
		/*
		 * The reason I'm not simply calling decodeResource on the file path is because
		 * I want to enjoy the benefits of having different drawable densities automatically
		 * selected for me.
		 */
		final Bitmap bitmap;
		final int id = my_context.getResources().getIdentifier(the_path.split("\\.")[0], "drawable", 
				   my_context.getPackageName());
		if (id == 0) {
			bitmap = null;
		} else {
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 2;
			bitmap = BitmapFactory.decodeResource(my_context.getResources(), id, options);
		}
		if (id == 0 || bitmap == null) {
			throw new IllegalArgumentException("Could not find image at " + the_path);
		}
		return new AndroidImage(bitmap);
	}
	

}
